package GUI;

import Run_System.Equation;
import Run_System.Exercises;
import Run_System.ExervisesFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class InputPanel extends JPanel {
    private static final int DEFAULT_WIDTH = 400;
    private static final int DEFAULT_HEIGHT = 300;
    private int [] arr =  {100,0,50};
    public InputPanel(int pattern){
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
        setLayout(new BorderLayout());

        JPanel outPanel = new JPanel();

        JPanel inputPanel = new JPanel(new GridLayout(3,2,5,20));
        inputPanel.setPreferredSize(new Dimension(150,150));

        JLabel max_Label = new JLabel("算式最大范围",SwingConstants.RIGHT);  //输入算式最大范围
        final  JTextField max_jt = new JTextField(5);

        JLabel min_Label = new JLabel("算式最小范围",SwingConstants.RIGHT); //输入算式最小范围
        final JTextField min_jt = new JTextField(5);

        JLabel size_Label = new JLabel("习题数量",SwingConstants.RIGHT);   //输入习题数量
        final JTextField size_jt = new JTextField(5);

        inputPanel.add(max_Label);
        inputPanel.add(max_jt);
        inputPanel.add(min_Label);
        inputPanel.add(min_jt);
        inputPanel.add(size_Label);
        inputPanel.add(size_jt);

        JButton insertButton = new JButton("开始做题吧，小明");
        insertButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {      //获取输入数据
                try {
                    arr[0] = Integer.parseInt(max_jt.getText());
                    arr[1] = Integer.parseInt(min_jt.getText());
                    arr[2] = Integer.parseInt(size_jt.getText());
                    Equation.setRange(arr[0], arr[1]);
                    DisplayExercisesPanel panel = new DisplayExercisesPanel(ExervisesFactory.generateExercises(pattern, arr[2]), new ArrayList<Integer>());
                    removeAll();
                    add(panel);
                    validate();
                }catch (Exception ex){
                    JOptionPane.showMessageDialog(null,"要输入完整的信息哦","坚持就是胜利",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(insertButton);
        add(new JLabel(" "),BorderLayout.NORTH);
        add(buttonPanel,BorderLayout.SOUTH);
        outPanel.add(inputPanel);
        add(outPanel,BorderLayout.CENTER);

    }


    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(852,543);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(new InputPanel(1));
        frame.setVisible(true);
    }
}
